--[[ local Info about this version ]]-- 
local TITLE				=	"InvitedByFriend";
local VERSION			=	0.5;
local INTERFACE			=	"2.0.8.1843";
local RELEASE_DATE		=	"22/06-2009";
local AUTHOR			=	"Zilvermoon";
local ADDON_LOADED		=	TITLE.." v"..VERSION;

function IBF_OnLoad(frame)
	frame:RegisterEvent("VARIABLES_LOADED");
	frame:RegisterEvent("PARTY_INVITE_REQUEST");
	SaveVariablesPerCharacter("InvitedByFriend");
	SLASH_IBF_SlashHandler1 = "/ibf";
	SlashCmdList["IBF_SlashHandler"] = IBF_SlashHandler;
end

function IBF_OnEvent(event)
	if event == "VARIABLES_LOADED" then
		IBF_VarLoaded();
		return;
	end
	if event == "PARTY_INVITE_REQUEST" then
		IBF_FriendRequest(event, arg1);
		return;
	end
end

function IBF_FriendRequest(event, arg1)
	IBFDebug("IBF(event, arg1)");
	IBFDebug(event..", "..arg1);
	IBFDebug(StaticPopup_Visible("PARTY_INVITE"));
	local NotInParty = GetNumPartyMembers();
	if ( NotInParty == 0) then
		if arg1 then
			if InvitedByFriend["FriendState"] == "Active" then
				if GetFriendNames(arg1) then
					IBF_AcceptPartyRequest();
					return;
				end
			end
			if InvitedByFriend["GuildState"] == "Active" then
				if GetGuildNames(arg1) then
					IBF_AcceptPartyRequest();
					return;
				end
			end
			if InvitedByFriend["CustomState"] == "Active" then
				if GetCustomNames(arg1) then
					IBF_AcceptPartyRequest();
					return;
				end
			end
			if InvitedByFriend["AllState"] == "Active" then
				IBF_AcceptPartyRequest();
				return;
			end
		end
	end
end

function IBF_AcceptPartyRequest()
	local invitepopup = StaticPopup_Visible("PARTY_INVITE")
	if invitepopup == "StaticPopup1" then
		StaticPopup_OnClick(StaticPopup1, 1);
	end
	if invitepopup == "StaticPopup2" then
		StaticPopup_OnClick(StaticPopup2, 1);
	end
	if invitepopup == "StaticPopup3" then
		StaticPopup_OnClick(StaticPopup3, 1);
	end
	if invitepopup == "StaticPopup4" then
		StaticPopup_OnClick(StaticPopup4, 1);
	end
end

function GetFriendNames(fName)
	IBFDebug("function GetFriendNames was found");
	local index = 0;
	local friendcount = GetFriendCount("Friend");
	IBFDebug("friendcount: "..friendcount);
	for i = 1, friendcount, 1 do
		local name, _, _, _ ,_, _, _, _, _, _ = GetFriendInfo("Friend", i );
		IBFDebug("name: "..name);
		if (name == fName) then
			IBFDebug("true");
			return true;
		end
		index = index+1;
	end
	IBFDebug(index.." Friend's was read");
	IBFDebug("false");
	return false;
end

function GetGuildNames(gName)
	IBFDebug("function GetGuildNames was found");
	local index = 0;
	local numGuildNodes = GetNumGuildMembers();
	IBFDebug("numGuildNodes: "..numGuildNodes);
	for i = 1, numGuildNodes do
		local name, _, _, _, _, _, _, _, _, _, _, _, _, _ = GetGuildRosterInfo(i);
		IBFDebug("name: "..name);
		if (name == gName) then
			IBFDebug("true");
			return true;
		end
		index = index+1;
	end
	IBFDebug(index.." Guildmember's was read");
	IBFDebug("false");
	return false;
end

function GetCustomNames(gName)
	IBFDebug("function GetCustomNames was found");
	local index = 0;
	for k, v in pairs(InvitedByFriend["CustomList"]) do
		IBFDebug("name: "..v);
		if (string.lower(v) == string.lower(gName)) then
			IBFDebug("true");
			return true;
		end
		index = index+1;
	end
	IBFDebug(index.." CustomList names was read");
	IBFDebug("false");
	return false;
end

function IBF_VarLoaded()
	if( not InvitedByFriend ) then
		InvitedByFriend = {};
	end
	if( not InvitedByFriend["DebugState"]) then
		InvitedByFriend["DebugState"] = "Deactive";
	end
	if( not InvitedByFriend["FriendState"]) then
		InvitedByFriend["FriendState"] = "Deactive";
	end
	if( not InvitedByFriend["GuildState"]) then
		InvitedByFriend["GuildState"] = "Deactive";
	end
	if( not InvitedByFriend["CustomState"]) then
		InvitedByFriend["CustomState"] = "Deactive";
		InvitedByFriend["CustomList"] = {};
	end
	if( not InvitedByFriend["AllState"]) then
		InvitedByFriend["AllState"] = "Deactive";
	end
	if( InvitedByFriend["Version"] ~= VERSION) then
		InvitedByFriend["Version"] = VERSION;
	end
end

function IBF_ToggleFriendState()
	if InvitedByFriend["FriendState"] == "Active" then
		InvitedByFriend["FriendState"] = "Deactive";
	else
		InvitedByFriend["FriendState"] = "Active";
	end
	IBF_PrintState("friend");
end

function IBF_ToggleGuildState()
	if InvitedByFriend["GuildState"] == "Active" then
		InvitedByFriend["GuildState"] = "Deactive";
	else
		InvitedByFriend["GuildState"] = "Active";
	end
	IBF_PrintState("guild");
end

function IBF_ToggleCustomState()
	if InvitedByFriend["CustomState"] == "Active" then
		InvitedByFriend["CustomState"] = "Deactive";
	else
		InvitedByFriend["CustomState"] = "Active";
	end
	IBF_PrintState("custom");
end

function IBF_ToggleAllState()
	if InvitedByFriend["AllState"] == "Active" then
		InvitedByFriend["AllState"] = "Deactive";
	else
		InvitedByFriend["AllState"] = "Active";
	end
	IBF_PrintState("All");
end

function IBF_ToggleDebugState()
	if InvitedByFriend["DebugState"] == "Active" then
		InvitedByFriend["DebugState"] = "Deactive";
	else
		InvitedByFriend["DebugState"] = "Active";
	end
	IBF_PrintState("debug");
end

function IBF_PrintState(x)
	if x == "friend" then
		IBFMsg("|cffffffffIBF Friend Accept is |r"..IBF_StateColor(InvitedByFriend["FriendState"]));
	elseif x == "guild" then
		IBFMsg("|cffffffffIBF Guild Accept is |r"..IBF_StateColor(InvitedByFriend["GuildState"]));
	elseif x == "custom" then
		IBFMsg("|cffffffffIBF Custom Accept is |r"..IBF_StateColor(InvitedByFriend["CustomState"]));
	elseif x == "All" then
		IBFMsg("|cffffffffIBF All Accept is |r"..IBF_StateColor(InvitedByFriend["AllState"]));
	elseif x == "debug" then
		IBFMsg("|cffffffffIBF Debug is |r"..IBF_StateColor(InvitedByFriend["DebugState"]));
	elseif x == "all" then
		IBFMsg("|cffffffffIBF Friend Accept is |r"..IBF_StateColor(InvitedByFriend["FriendState"]));
		IBFMsg("|cffffffffIBF Guild Accept is |r"..IBF_StateColor(InvitedByFriend["GuildState"]));
		IBFMsg("|cffffffffIBF Custom Accept is |r"..IBF_StateColor(InvitedByFriend["CustomState"]));
		IBFMsg("|cffffffffIBF All Accept is |r"..IBF_StateColor(InvitedByFriend["AllState"]));
		IBFMsg("|cffffffffIBF Debug is |r"..IBF_StateColor(InvitedByFriend["DebugState"]));
	end
end

function IBF_StateColor(TABLE)
	if TABLE == "Active" then
		TABLE = "|cff00ff00"..TABLE.."|r";
	else
		TABLE = "|cffff0000"..TABLE.."|r";
	end
	return TABLE;
end

--[[ SHOW CUSTOMLIST CONTENSE ]]-- 
function IBF_ShowCustomList()
	local Empty = true;
	IBFMsg("CustomList:");
	for k, v in pairs(InvitedByFriend["CustomList"]) do 
		IBFMsg(v);
		Empty = false;
	end
	if (Empty == true) then
	IBFMsg("EMPTY");
	end
end

--[[ ADD $name TO CUSTOMLIST ]]-- 
function IBF_AddToCustomList(DATA)
	local exist = false;
	for k, v in pairs(InvitedByFriend["CustomList"]) do
		if (v == DATA) then
			exist = true;
			break;
		end
	end
	if exist == true then
		-- print Error, it's already in table
		IBFMsg(DATA.." already exist in CustomList");
	else
		table.insert(InvitedByFriend["CustomList"], tostring(DATA));
		IBFMsg(DATA.." added to CustomList");
	end
end

--[[ REMOVE $name FROM CUSTOMLIST ]]-- 
function IBF_RemoveFromCustomList(DATA)
	local exist = false;
	local Del;
	for k, v in pairs(InvitedByFriend["CustomList"]) do
		if (v == DATA) then
			exist = true;
			table.remove(InvitedByFriend["CustomList"], k);
			IBFMsg(DATA.." removed from CustomList");
			break;
		end
	end
	if exist == false then
		-- print Error, didn't exist in table
		IBFMsg("Unable to find "..DATA.." in CustomList")
	end
end

function IBF_SlashHandler(editBox, msg)
	if msg == "" then
		IBF_NoMSG();
	end

	local Msg, SubMsg = IBF_GetSubCommand(msg);

	Msg = string.lower(Msg);
	if (SubMsg) then
		SubMsg = string.lower(SubMsg);
	end

	if (Msg == "friend") then
		IBF_ToggleFriendState();
	elseif (Msg == "guild") then
		IBF_ToggleGuildState();
	elseif (Msg == "custom") then
		IBF_ToggleCustomState();
	elseif (Msg == "list") then
		IBF_ShowCustomList();
	elseif (Msg == "add") then
		if (SubMsg) then
			IBF_AddToCustomList(SubMsg);
		end
	elseif (Msg == "remove") then
		if (SubMsg) then
			IBF_RemoveFromCustomList(SubMsg);
		end
	elseif (Msg == "all") then
		IBF_ToggleAllState();
	elseif (Msg == "debug") then
		IBF_ToggleDebugState();
	elseif (Msg == "status") then
		IBF_PrintState("all");
	elseif (Msg == "help") then
		IBF_NoMSG();
	end
	IBFDebug("*"..Msg.."*");
end

function IBF_GetSubCommand(command)
	if command then
		local a, b = string.find(command, "%s");
		if a then
			local c = string.sub(command, 1, a-1);
			local d = string.sub(command, b+1);
			return c,d;
		else	
			return command,nil;
		end
	end
end

function IBF_NoMSG()
	IBFMsg("Invalid command, type:");
	IBFMsg("/ibf friend - to toggle Friend Accept");
	IBFMsg("/ibf guild - to toggle Guild Accept");
	IBFMsg("/ibf custom - to toggle Custom Accept");
	IBFMsg("/ibf list - to show CustomList in chat");
	IBFMsg("/ibf add $name - to add $name to CustomList");
	IBFMsg("/ibf remove $name - to remove $name from CustomList");
	IBFMsg("/ibf all - to toggle All Accept");
	IBFMsg("/ibf debug - to toggle Debug Mode");
	IBFMsg("/ibf status - to show status's");
	IBFMsg("/ibf help - to show this message");
end

function IBFMsg(msg,red,green,blue)
	if ((red) and (green) and (b)) then
		DEFAULT_CHAT_FRAME:AddMessage(msg, red, green, blue);
	else
		DEFAULT_CHAT_FRAME:AddMessage(msg);
	end
end

function IBFDebug(msg,red,green,blue)
	if InvitedByFriend["DebugState"] == "Active" then
		if ((red) and (green) and (b)) then
			IBFMsg(msg, red, green, blue);
		else
			IBFMsg(msg);
		end
	end
end
